package com.xysq.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.rockcent.action.CallbackListener;
import com.rockcent.model.DeliveryModel;
import com.umeng.analytics.MobclickAgent;
import com.xysq.lemon.R;
import com.xysq.util.ToastUtil;

import java.text.SimpleDateFormat;

import butterknife.InjectView;


public class ConfirmReceivingActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.btn_confirm_receive)
    Button confirmReceiveBtn;
    @InjectView(R.id.txt_delivery_company)
    TextView deliveryCompanyTxt;
    @InjectView(R.id.txt_delivery_number)
    TextView deliveryNumberTxt;
    @InjectView(R.id.txt_delivery_receive_name)
    TextView deliveryReceiveNameTxt;
    @InjectView(R.id.txt_delivery_receive_phone)
    TextView deliveryReceivePhoneTxt;
    @InjectView(R.id.txt_delivery_receive_address)
    TextView deliveryReceiveAddressTxt;
    @InjectView(R.id.txt_delivery_receive_remark)
    TextView deliveryReceiveRemarkTxt;
    @InjectView(R.id.txt_delivery_send_number)
    TextView deliverySendNumberTxt;
    @InjectView(R.id.txt_delivery_send_time)
    TextView deliverySendTimeTxt;
    @InjectView(R.id.txt_delivery_send_auto_confirm)
    TextView deliverySendAutoConfirmTxt;
    @InjectView(R.id.ibtn_back)
    ImageButton backIbtn;

    int verificationId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_receiving);
        getExtra();
        initViewAndData();
        loadData();

    }

    private void loadData() {
        appAction.getVerificationDelivery(verificationId, new CallbackListener<DeliveryModel>() {
            @Override
            public void onSuccess(DeliveryModel data) {
                deliveryCompanyTxt.setText("快递公司: " + data.getDeliveryName());
                deliveryNumberTxt.setText("运单号: " + data.getDeliveryNo());
                deliveryReceiveNameTxt.setText(data.getAddressee());
                deliveryReceivePhoneTxt.setText(data.getLinkphone());
                deliveryReceiveAddressTxt.setText("地址: " + data.getAddress());
                deliveryReceiveRemarkTxt.setText(data.getRemark());
                deliverySendNumberTxt.setText(data.getOrderNo());
                deliverySendTimeTxt.setText(timeChangeFormat(Long.parseLong(data.getCreatetime())));
                deliverySendAutoConfirmTxt.setText(String.valueOf(timeChangeFormat(data.getAutoEndTime())));
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showShort(ConfirmReceivingActivity.this, "获取订单信息失败");
            }
        });
    }

    private void getExtra() {
        Intent intent = getIntent();
        verificationId = intent.getIntExtra("verificationId", 0);
    }

    private void initViewAndData() {
        confirmReceiveBtn.setOnClickListener(this);
        backIbtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view == confirmReceiveBtn) {
            confirmReceive();
        }
        if(view == backIbtn){
            finish();
        }
    }

    private void confirmReceive() {
        AlertDialog.Builder builder = new AlertDialog.Builder(ConfirmReceivingActivity.this);
        builder.setTitle(R.string.dialog_sure_receive);
        builder.setNegativeButton(R.string.dialog_action_cancel,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        builder.setPositiveButton(R.string.dialog_action_confirm,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        receive();
                        dialog.dismiss();
                    }
                });
        builder.show();
    }

    private void receive() {
        appAction.endVerificationDelivery(verificationId, new CallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                ToastUtil.showShort(ConfirmReceivingActivity.this, R.string.toast_end_verification);
                finish();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showShort(ConfirmReceivingActivity.this, R.string.toast_end_verification_fail);
            }
        });
    }

    private String timeChangeFormat(Long data) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String time = format.format(data);
        return time;
    }
}
